using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using ScreenManager;
using LabyrinthProject;

namespace LabirintGUI.Screens
{
    class SettingsScreen : ScreenManager.Screen
    {
        
        SpriteBatch MainSpriteBatch;
        Texture2D Background;
        MouseHandler ourMouse; //An instance of the MouseHandler class
        List<Button> buttonlist; //Our list of buttons
        MouseState mouseState; //MouseState needed for what checking 

        Button dotButtonRightHigh; //An instance of Button        
        Button dotButtonRightMedium; //An instance of Button       
        Button dotButtonRightLow; //An instance of Button        
        Button dotButtonLeftHigh; //An instance of Button        
        Button dotButtonLeftMedium; //An instance of Button       
        Button dotButtonLeftLow; //An instance of Button 
        Button leftButton; //An instance of Button 
        Button rightButton; //An instance of Button  
 

        Button soundButton;
        Button musicButton;
        Button fullScreenButton;

        Vector2 resolution;
        Boolean soundOn;
        Boolean musicOn;
        Boolean fullScreenOn;

        int resolutionNum;
        String resolutionString;
        SpriteFont font;
        Vector2 fontPos;

        Texture2D textureON;
        Texture2D textureONHovered;
        Texture2D textureOFF;
        Texture2D textureOFFHovered;
 

        public SettingsScreen(GraphicsDevice device)
            : base(device, "settings")
        {
        }

        public override bool Init(Game game)
        {

            Background = game.Content.Load<Texture2D>(@"settings");

            resolutionNum = 0;
            resolutionString = "1280x768";
            font = game.Content.Load<SpriteFont>("myFont");
            fontPos = new Vector2(1460 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , 1100 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            soundOn = true;
            musicOn = true;
            fullScreenOn = true;
            resolution = new Vector2(1280, 768);

            Texture2D textureDot = game.Content.Load<Texture2D>(@"dot_button");
            Texture2D textureDotHovered = game.Content.Load<Texture2D>(@"dot_button_hovered");
            Texture2D textureDotClicked = game.Content.Load<Texture2D>(@"dot_button_clicked");
            textureON = game.Content.Load<Texture2D>(@"ON_button");
            textureONHovered = game.Content.Load<Texture2D>(@"ON_button_hovered");
            textureOFF = game.Content.Load<Texture2D>(@"OFF_button");
            textureOFFHovered = game.Content.Load<Texture2D>(@"OFF_button_hovered");
            Texture2D textureLeft = game.Content.Load<Texture2D>(@"left_button");
            Texture2D textureLeftHovered = game.Content.Load<Texture2D>(@"left_button_hovered");
            Texture2D textureRight = game.Content.Load<Texture2D>(@"right_button");
            Texture2D textureRightHovered = game.Content.Load<Texture2D>(@"right_button_hovered");

            
            Rectangle rectangleDotRightHigh = new Rectangle((int)buttonRH.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
                (int)buttonRH.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                 textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotRightMedium = new Rectangle((int)buttonRM.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonRM.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotRightLow = new Rectangle((int)buttonRL.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonRL.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotLeftHigh = new Rectangle((int)buttonLH.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonLH.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotLeftMedium = new Rectangle((int)buttonLM.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonLM.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleDotLeftLow = new Rectangle((int)buttonLL.X * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth - (textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth) / 2,
               (int)buttonLL.Y * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight - (textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight) / 2,
                textureDot.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureDot.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleSound = new Rectangle(1360 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth ,
               429 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight ,
                textureON.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureON.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Rectangle rectangleMusic = new Rectangle(1360 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth,
               651 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight,
                textureON.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureON.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);
            Rectangle rectangleFullScreen = new Rectangle(1360 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth,
               869 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight,
                textureON.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureON.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleLeft = new Rectangle((int)1410 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth,
               (int)1206 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight,
                textureLeft.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
               , textureLeft.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);

            Rectangle rectangleRight = new Rectangle((int)1750 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth,
                (int)1206 * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight,
                textureRight.Width * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / Properties.backgroundWidth
                , textureRight.Height * GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / Properties.backgroundHeight);


            dotButtonRightHigh = new Button(rectangleDotRightHigh, textureDot, textureDotHovered, textureDotClicked, "RH");
            dotButtonRightMedium = new Button(rectangleDotRightMedium, textureDot, textureDotHovered, textureDotClicked, "RM");
            dotButtonRightLow = new Button(rectangleDotRightLow, textureDot, textureDotHovered, textureDotClicked, "RL");
            dotButtonLeftHigh = new Button(rectangleDotLeftHigh, textureDot, textureDotHovered, textureDotClicked, "LH");
            dotButtonLeftMedium = new Button(rectangleDotLeftMedium, textureDot, textureDotHovered, textureDotClicked, "LM");
            dotButtonLeftLow = new Button(rectangleDotLeftLow, textureDot, textureDotHovered, textureDotClicked, "LL");

            leftButton = new Button(rectangleLeft, textureLeft, textureLeftHovered, textureLeftHovered, "left");
            rightButton = new Button(rectangleRight, textureRight, textureRightHovered, textureRightHovered, "right");

            soundButton = new Button(rectangleSound, textureON, textureONHovered, textureONHovered, "sound");
            musicButton = new Button(rectangleMusic, textureON, textureONHovered, textureONHovered, "music");
            fullScreenButton = new Button(rectangleFullScreen, textureON, textureONHovered, textureONHovered, "fullScreen");

            buttonlist = new List<Button>(); //Actually creating the list
            this.buttonlist.Add(dotButtonRightHigh);
            this.buttonlist.Add(dotButtonRightMedium);
            this.buttonlist.Add(dotButtonRightLow);
            this.buttonlist.Add(dotButtonLeftHigh);
            this.buttonlist.Add(dotButtonLeftMedium);
            this.buttonlist.Add(dotButtonLeftLow);
            this.buttonlist.Add(soundButton);
            this.buttonlist.Add(musicButton);
            this.buttonlist.Add(fullScreenButton);
            this.buttonlist.Add(leftButton);
            this.buttonlist.Add(rightButton);

            ourMouse = new MouseHandler(new Vector2(0, 0), game.Content.Load<Texture2D>(@"pointer"));

            

            return base.Init(game);
        }



        

        public override void Shutdown()
        {
            base.Shutdown();
        }



        public override void Draw(GameTime gameTime)
        {

            MainSpriteBatch = new SpriteBatch(_device);

            this.MainSpriteBatch.Begin();
            Vector2 position = new Vector2(0, 0);
            Rectangle backgroundRectangle = new Rectangle(0, 0, _device.Viewport.Width, _device.Viewport.Height);
            this.MainSpriteBatch.Draw(Background, backgroundRectangle, Color.White);

            MainSpriteBatch.DrawString(font, resolutionString, fontPos, Color.Black);

            foreach (Button b in buttonlist) //For every button run through the code between the {}
            {
                //For every button in our list, draw it
                b.Draw(MainSpriteBatch);

                if (b.hovering) //If we are hovering
                {
                    //Draw the hover sprite
                    MainSpriteBatch.Draw(b.OnHovering, b.position, Color.White );
                }
                if (b.clicking) //If we are clicking
                {
                    //Draw the clicking sprite.
                    MainSpriteBatch.Draw(b.OnClicking, b.position, Color.White);
                    switch (b.name)
                    {
                        case "LM":
                            SCREEN_MANAGER.goto_screen("main");
                            break;
                        case "RM":
                            Properties.fullScreenOn = fullScreenOn;
                            Properties.musicOn = musicOn;
                            Properties.soundOn = soundOn;
                            Properties.resolution = resolution;
                            SCREEN_MANAGER.goto_screen("main");
                            break;
                        case "music":
                            if (musicOn)
                            {
                                musicButton.Tex = textureOFF;
                                musicButton.OnHovering = textureOFFHovered;
                                musicButton.OnClicking = textureOFFHovered;
                                musicOn = false;
                                System.Threading.Thread.Sleep(200);
                            }
                            else
                            {
                                musicButton.Tex = textureON;
                                musicButton.OnHovering = textureONHovered;
                                musicButton.OnClicking = textureONHovered;
                                musicOn = true;
                                System.Threading.Thread.Sleep(200);
                            }
                            break;
                        case "sound":
                            if (soundOn)
                            {
                                soundButton.Tex = textureOFF;
                                soundButton.OnHovering = textureOFFHovered;
                                soundButton.OnClicking = textureOFFHovered;
                                soundOn = false;
                                System.Threading.Thread.Sleep(200);
                            }
                            else
                            {
                                soundButton.Tex = textureON;
                                soundButton.OnHovering = textureONHovered;
                                soundButton.OnClicking = textureONHovered;
                                soundOn = true;
                                System.Threading.Thread.Sleep(200);
                            }
                            break;
                        case "fullScreen":
                            if (fullScreenOn)
                            {
                                fullScreenButton.Tex = textureOFF;
                                fullScreenButton.OnHovering = textureOFFHovered;
                                fullScreenButton.OnClicking = textureOFFHovered;
                                fullScreenOn = false;
                                System.Threading.Thread.Sleep(200);
                            }
                            else
                            {
                                fullScreenButton.Tex = textureON;
                                fullScreenButton.OnHovering = textureONHovered;
                                fullScreenButton.OnClicking = textureONHovered;
                                fullScreenOn = true;
                                System.Threading.Thread.Sleep(200);
                            }
                            break;
                        case "left":
                            changeResolution(false);
                            System.Threading.Thread.Sleep(100);
                            break;
                        case "right":
                            changeResolution(true);
                            System.Threading.Thread.Sleep(100);
                            break;
                        
                    }     
                }
            }
            //Draw the mouse.
            ourMouse.Draw(MainSpriteBatch);
            //Done
            MainSpriteBatch.End();
            


            base.Draw(gameTime);
        }

        private void changeResolution(bool p)
        {
            int tmp;
            if (p)
            {
                tmp = (resolutionNum + 1) % 3;
            }
            else
            {
                tmp = (resolutionNum + 2) % 3;
            }

            resolutionNum=tmp;
            switch (resolutionNum)
            {
                case 0:
                    resolution = new Vector2(1280, 768);
                    resolutionString = "1280x768";
                    break;
                case 1:
                    resolution = new Vector2(1440, 900);
                    resolutionString = "1440x900";
                    break;
                case 2:
                    resolution = new Vector2(800, 600);
                    resolutionString = "800x600";
                    break;
            }
        }

        private void HandleMouse()
        {
            mouseState = Mouse.GetState(); //Get the current state of the mouse
            foreach (Button b in buttonlist)
            {                     
                if (ourMouse.ButtonClick(b)) //There's only one button, so we just hardcode it.
                    //If we're hovering over the mouse
                    b.hovering = true; //We ARE hovering
                else
                    b.hovering = false; //Not hovering.
                //We don't even need to use ButtonClick() again if we know if we're hovering.
                if (mouseState.LeftButton == ButtonState.Pressed && b.hovering) //If we're clicking with the Left Mouse Button and we're over the button.
                    b.clicking = true; //We ARE clicking
                else
                    b.clicking = false; //Not clicking
            }
        }


        public override void Update(GameTime gameTime)
        {
 
            // Check if m is pressed and go to screen2
           if (Keyboard.GetState().IsKeyDown(Keys.Back))
            {
                SCREEN_MANAGER.goto_screen("main");
            }
            HandleMouse(); //Check clicking
            ourMouse.Update();
            base.Update(gameTime);
        }
    }
}
